/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.controller.ProjectsHistoryJpaController;
import model.controller.exceptions.NonexistentEntityException;
import model.db.ProjectsHistory;
import structure.ComboModelID;


/**
 *
 * @author mwiatrowski
 */
public class DatabaseController extends ProjectsHistoryJpaController {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("FlowOfPeopleClientPU");
    private ProjectsHistory area;
 
    public DatabaseController() {
        super(emf);
    }

   
    public HashMap<String, ComboModelID> getMap() {
        List<ProjectsHistory> projectsHistory = findProjectsHistoryEntities();
        HashMap<String, ComboModelID> map = new HashMap<>();
        for (ProjectsHistory p : projectsHistory) {
            String key = "Id: "+p.getId()+", Opis:"+p.getDescription();
            ComboModelID value = new ComboModelID(p.getId(), "Id: "+p.getId()+", Opis:"+p.getDescription());
            map.put(key, value);
        }
        return map;
    }

    public void incrementEntry(BigDecimal id) {

        try {
            area = new ProjectsHistory();
            area = findProjectsHistory(id);
            BigDecimal i = area.getCountEntry();
            i = i.add(new BigDecimal(1));
            area.setCountEntry(i);
            edit(area);

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void incrementExit(BigDecimal id) {
        try {
            area = new ProjectsHistory();
            area = findProjectsHistory(id);
            BigDecimal i = area.getCountExit();
            i = i.add(new BigDecimal(1));
            area.setCountExit(i);
            edit(area);

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(DatabaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DatabaseController dbContr = new DatabaseController();
                dbContr.incrementEntry(new BigDecimal(1));

            }
        });
    }
}


